

#include "main.h"

/********************************UART0**********************************/
/* 波特率计算公式：baudrate=baud clock / ((OSR+1) × SBR)=40MHz/((15+1)*22)=113636≈115200*/
/* 115200 baud, 1 stop bit, 8 bit format, no parity */
Bsw_Lpuart_Cfg 	LPUART0_Cfg = 
{
	.base 				= LPUART0,												
	.clockName 		= LPUART0_CLK,
	.clockSource 	= 6,        /*PLLCLK_DIV2=40MHz*/
	.OSRatio			=	15,
	.BaudRate			=	22,  /*baudrate=baud clock / ((OSR+1) × SBR)*/
	.DataBit			=	LPUART_8_BITS_PER_CHAR,
	.StopBit			=	LPUART_ONE_STOP_BIT,
	.PrtMode			=	LPUART_PARITY_DISABLED,
	.RxDmaCmd			=	true,
	.TxDmaCmd			=	true,
	.RxWater			= 3,
	.TxWater			= 1,
	.BothEgSCmd   = false,
	.RxCmd				=	true,
	.TxCmd				=	true,
};



BSW_Dma_Cfg Uart0_TxDma =
{
	.DMA_Ch 						=	DMA_CH11,			/*配置DMA通道    						eg: DMA_CH9										*/
	.IRQn								=	DMA11_IRQn,		/*配置DMA通道中断						eg: DMA9_IRQn									*/
	/*.SrcAddr					=	(u32)TCD0_Source,*/					/*配置源地址								eg: (u32)Dma_Src							*/
	.SrcOffset					=	1,													/*自动累加源地址大小				eg: 1 每次传输地址自动加1			*/
	.SrcTrfSize 				=	EDMA_TRANSFER_SIZE_1B,			/*源地址单次传输几个字节		eg:	EDMA_TRANSFER_SIZE_1B 		*/
	.SrcLastAdj       	=	0,													/*配置传输完成时是否更改源地址	eg: 0 不更改源地址				*/
	
	/*.DestAddr					=	(u32)TCD0_Dest,*/						/*配置目标地址							eg: (u32)Dma_Dest							*/
	.DestOffset					=	0,													/*自动累加目标地址大小			eg: 1 每次传输地址自动加1			*/
	.DestTrfSize				=	EDMA_TRANSFER_SIZE_1B,			/*目标地址单次传输几个字节	eg:	EDMA_TRANSFER_SIZE_1B 		*/
	.DestLastAdj      	=	0,													/*配置传输完成时是否更改目标地址	eg: 0 不更改目标地址		*/
		
	.SetNbytes					=	1,													/*DMA开启每次传输几个字节		eg: 1	每次传输1个字节					*/
	.SetMjrCount				=	5,													/*DMA开启每次循环的次数 		eg: 5 每次传输多少次					*/
	.b_MjrCpltIrq				=	true,												/*配置是否传输完成中断																		*/
	.b_HlfCpltIrq				=	false,
	.b_DmaRqstDone			=	true,												/*传输结束是否自动关闭DMA		eg: true 自动关闭DMA 					*/
	.b_Dma_En						=	true,												/* DMA使能配置							eg: true 使能DMA触发传输			*/
};

BSW_Dma_Cfg Uart0_RxDma =
{
	.DMA_Ch 						=	DMA_CH10,				/*配置DMA通道    						eg: DMA_CH8										*/
	.IRQn								=	DMA10_IRQn,			/*配置DMA通道中断						eg: DMA8_IRQn									*/
	/*.SrcAddr					=	(u32)TCD0_Source,*/					/*配置源地址								eg: (u32)Dma_Src							*/
	.SrcOffset					=	0,													/*自动累加源地址大小				eg: 1 每次传输地址自动加1			*/
	.SrcTrfSize 				=	EDMA_TRANSFER_SIZE_1B,			/*源地址单次传输几个字节		eg:	EDMA_TRANSFER_SIZE_1B 		*/
	.SrcLastAdj       	=	0,													/*配置传输完成时是否更改源地址	eg: 0 不更改源地址				*/
	
	/*.DestAddr					=	(u32)TCD0_Dest,*/						/*配置目标地址							eg: (u32)Dma_Dest							*/
	.DestOffset					=	1,													/*自动累加目标地址大小			eg: 1 每次传输地址自动加1			*/
	.DestTrfSize				=	EDMA_TRANSFER_SIZE_1B,			/*目标地址单次传输几个字节	eg:	EDMA_TRANSFER_SIZE_1B 		*/
	.DestLastAdj      	=	-5,													/*配置传输完成时是否更改目标地址	eg: 0 不更改目标地址		*/
		
	.SetNbytes					=	1,													/*DMA开启每次传输几个字节		eg: 1	每次传输1个字节					*/
	.SetMjrCount				=	5,													/*DMA开启每次循环的次数 		eg: 6 每次传输多少次					*/
	.b_MjrCpltIrq				=	true,												/*配置是否传输完成中断																		*/
	.b_HlfCpltIrq				=	false,
	.b_DmaRqstDone			=	false,												/*传输结束是否自动关闭DMA		eg: true 自动关闭DMA 					*/
	.b_Dma_En						=	true,												/* DMA使能配置							eg: true 使能DMA触发传输			*/
};

/********************************UART1**********************************/
Bsw_Lpuart_Cfg 	LPUART1_Cfg = 
{
	.base 				= LPUART1,												
	.clockName 		= LPUART1_CLK,
	.clockSource 	= 6,
	.OSRatio			=	15,
	.BaudRate			=	22,
	.DataBit			=	LPUART_8_BITS_PER_CHAR,
	.StopBit			=	LPUART_ONE_STOP_BIT,
	.PrtMode			=	LPUART_PARITY_DISABLED,
	.RxDmaCmd			=	true,
	.TxDmaCmd			=	true,
	.RxWater			= 3,
	.TxWater			= 1,
	.BothEgSCmd   = false,
	.RxCmd				=	true,
	.TxCmd				=	true,
};



BSW_Dma_Cfg Uart1_TxDma =
{
	.DMA_Ch 						=	DMA_CH9,										/*配置DMA通道    						eg: DMA_CH9										*/
	.IRQn								=	DMA9_IRQn,									/*配置DMA通道中断						eg: DMA9_IRQn									*/
	/*.SrcAddr					=	(u32)TCD0_Source,*/					/*配置源地址								eg: (u32)Dma_Src							*/
	.SrcOffset					=	1,													/*自动累加源地址大小				eg: 1 每次传输地址自动加1			*/
	.SrcTrfSize 				=	EDMA_TRANSFER_SIZE_1B,			/*源地址单次传输几个字节		eg:	EDMA_TRANSFER_SIZE_1B 		*/
	.SrcLastAdj       	=	0,													/*配置传输完成时是否更改源地址	eg: 0 不更改源地址				*/
	
	/*.DestAddr					=	(u32)TCD0_Dest,*/						/*配置目标地址							eg: (u32)Dma_Dest							*/
	.DestOffset					=	0,													/*自动累加目标地址大小			eg: 1 每次传输地址自动加1			*/
	.DestTrfSize				=	EDMA_TRANSFER_SIZE_1B,			/*目标地址单次传输几个字节	eg:	EDMA_TRANSFER_SIZE_1B 		*/
	.DestLastAdj      	=	0,													/*配置传输完成时是否更改目标地址	eg: 0 不更改目标地址		*/
		
	.SetNbytes					=	1,													/*DMA开启每次传输几个字节		eg: 1	每次传输1个字节					*/
	.SetMjrCount				=	5,													/*DMA开启每次循环的次数 		eg: 5 每次传输多少次					*/
	.b_MjrCpltIrq				=	true,												/*配置是否传输完成中断																		*/
	.b_HlfCpltIrq				=	false,
	.b_DmaRqstDone			=	true,												/*传输结束是否自动关闭DMA		eg: true 自动关闭DMA 					*/
	.b_Dma_En						=	true,												/* DMA使能配置							eg: true 使能DMA触发传输			*/
};

BSW_Dma_Cfg Uart1_RxDma =
{
	.DMA_Ch 						=	DMA_CH8,										/*配置DMA通道    						eg: DMA_CH8										*/
	.IRQn								=	DMA8_IRQn,									/*配置DMA通道中断						eg: DMA8_IRQn									*/
	/*.SrcAddr					=	(u32)TCD0_Source,*/					/*配置源地址								eg: (u32)Dma_Src							*/
	.SrcOffset					=	0,													/*自动累加源地址大小				eg: 1 每次传输地址自动加1			*/
	.SrcTrfSize 				=	EDMA_TRANSFER_SIZE_1B,			/*源地址单次传输几个字节		eg:	EDMA_TRANSFER_SIZE_1B 		*/
	.SrcLastAdj       	=	0,													/*配置传输完成时是否更改源地址	eg: 0 不更改源地址				*/
	
	/*.DestAddr					=	(u32)TCD0_Dest,*/						/*配置目标地址							eg: (u32)Dma_Dest							*/
	.DestOffset					=	1,													/*自动累加目标地址大小			eg: 1 每次传输地址自动加1			*/
	.DestTrfSize				=	EDMA_TRANSFER_SIZE_1B,			/*目标地址单次传输几个字节	eg:	EDMA_TRANSFER_SIZE_1B 		*/
	.DestLastAdj      	=	-5,													/*配置传输完成时是否更改目标地址	eg: 0 不更改目标地址		*/
		
	.SetNbytes					=	1,													/*DMA开启每次传输几个字节		eg: 1	每次传输1个字节					*/
	.SetMjrCount				=	5,													/*DMA开启每次循环的次数 		eg: 6 每次传输多少次					*/
	.b_MjrCpltIrq				=	true,												/*配置是否传输完成中断																		*/
	.b_HlfCpltIrq				=	false,
	.b_DmaRqstDone			=	false,												/*传输结束是否自动关闭DMA		eg: true 自动关闭DMA 					*/
	.b_Dma_En						=	true,												/* DMA使能配置							eg: true 使能DMA触发传输			*/
};

/*UART0 configure*/
Bsw_Port_Cfg 	PTB0_Cfg = 
{
	.Port_base	= PORTB,												
	.clockName	= PORTB_CLK,
	.pin	    = PIN0_IDX,
	.mux		= PORT_MUX_ALT2,
	.intConfig	=	PORT_DMA_FALLING_EDGE,
};

Bsw_Port_Cfg 	PTB1_Cfg = 
{
	.Port_base	= PORTB,												
	.clockName	= PORTB_CLK,
	.pin		= PIN1_IDX,
	.mux		= PORT_MUX_ALT2,
	.intConfig	=	PORT_DMA_FALLING_EDGE,
};
/*UART1 configure*/
Bsw_Port_Cfg 	PTC8_Cfg = 
{
	.Port_base	= PORTC,												
	.clockName	= PORTC_CLK,
	.pin	    = PIN8_IDX,
	.mux		= PORT_MUX_ALT2,
	.intConfig	=	PORT_DMA_FALLING_EDGE,
};

Bsw_Port_Cfg 	PTC9_Cfg = 
{
	.Port_base	= PORTC,												
	.clockName	= PORTC_CLK,
	.pin		= PIN9_IDX,
	.mux		= PORT_MUX_ALT2,
	.intConfig	=	PORT_DMA_FALLING_EDGE,
};

void PORT_init (void) 
{
    //配置UART0端口
	BSW_GPIO_MUX_CONFIG(Bsw_Port_B,0,PORT_MUX_ALT2);			/* 	PB0: 	MUX = ALT2,UART0 TX */
	BSW_GPIO_MUX_CONFIG(Bsw_Port_B,1,PORT_MUX_ALT2);			/* 	PB1: 	MUX = ALT2,UART0 RX */

    //UART0 RS485_DE1
    BSW_GPIO_MUX_CONFIG(Bsw_Port_A,17,PORT_MUX_AS_GPIO);		/*	PA13:	MUX = ALT1, GPIO_PTA17 */
    BSW_GPIO_Direction(PTA,17,1);/*GPIO_PTA17设置为输出	RS485_DE */
    BSW_GPIO_SetBits(PTA,17);

    //配置UART1端口
    //BSW_PORT_CONFIG(PTC8_Cfg);	/* Port C8: MUX = ALT2,UART1 RX */
    //BSW_PORT_CONFIG(PTC9_Cfg);	/* Port C9: MUX = ALT2,UART1 TX */
    BSW_GPIO_MUX_CONFIG(Bsw_Port_C,8,PORT_MUX_ALT2);			/* 	PC8: 	MUX = ALT2,UART1 RX */
    BSW_GPIO_MUX_CONFIG(Bsw_Port_C,9,PORT_MUX_ALT2);			/* 	PC9: 	MUX = ALT2,UART1 TX */
    //UART1 RS485_DE2
    BSW_GPIO_MUX_CONFIG(Bsw_Port_E,7,PORT_MUX_AS_GPIO);		/*	PTE7:	MUX = ALT1, GPIO_PTE7 */
    BSW_GPIO_Direction(PTE,7,1);/*GPIO_PTE7设置为输出	RS485_DE */
    BSW_GPIO_SetBits(PTE,7);
    //BSW_GPIO_ResetBits(PTE,7);
}


u32 ECU_UART0_Tx_Add	= 0x00;
u32 ECU_UART0_Rx_Add	= 0x00;
u32 ECU_UART1_Tx_Add	= 0x00;
u32 ECU_UART1_Rx_Add	= 0x00;

u8 Uart0_Tx_Data[100] = {0x11,0xA5,0x00,0x5A,0xF0};
u8 Uart0_Rx_Data[101] = {0};
u8 Uart1_Tx_Data[100] = {0x11,0xA5,0x00,0x5A,0xF0};
u8 Uart1_Rx_Data[101] = {0};

uint32_t uart0_dmatxirq=0;
uint32_t uart0_dmarxirq=0;
uint32_t uart1_dmatxirq=0;
uint32_t uart1_dmarxirq=0;


void ECU_UART_INIT(void)
{
#if 0
    uint8_t  TransData1[] = {"\n\rHello World from TIC!\n\r"}; /* Transmit string & CR*/
    uint8_t rxdata1[4];

    LPUART1_PORT_init();
    LPUART_DMAMUX_Init();
    LPUART1_init();        /* Initialize LPUART @ 9600*/
    InitRXDMA((uint32_t *)&Uart1_Rx_Data[0],5); // Init a TCD to receive just 4 bytes
    InitTXDMA((uint32_t *)&Uart1_Tx_Data[0],5); // Init TCD to send a string
    DMA->SERQ = 1;    // enable DMA channel 1 HW trigger
    BSW_DMA_START(DMA_CH8);
    BSW_DMA_START(DMA_CH9);
    
	ECU_UART1_Tx_Add = (u32)Uart1_Tx_Data;
	ECU_UART1_Rx_Add = (u32)Uart1_Rx_Data;
    //while(!(DMA->TCD[DMA_CH9].CSR & DMA_TCD_CSR_DONE_MASK) ){}; // wait until the string is sent
#else
	PORT_init();
    /**************UART0 init********/
    BSW_LPUART_INIT(LPUART0_Cfg);

    ECU_UART0_Tx_Add = (u32)Uart0_Tx_Data;
    ECU_UART0_Rx_Add = (u32)Uart0_Rx_Data;

    BSW_DMAMUX_INIT(Uart0_TxDma.DMA_Ch,EDMA_REQ_LPUART0_TX);
    Uart0_TxDma.SrcAddr = ECU_UART0_Tx_Add;
    Uart0_TxDma.DestAddr = (u32)&(LPUART0->DATA);
    BSW_DMA_INIT_CFG(Uart0_TxDma);

    BSW_DMAMUX_INIT(Uart0_RxDma.DMA_Ch,EDMA_REQ_LPUART0_RX);
    Uart0_RxDma.SrcAddr = (u32)&(LPUART0->DATA);
    Uart0_RxDma.DestAddr = ECU_UART0_Rx_Add;
    BSW_DMA_INIT_CFG(Uart0_RxDma);

    /**************UART1 init********/
    BSW_LPUART_INIT(LPUART1_Cfg);

    ECU_UART1_Tx_Add = (u32)Uart1_Tx_Data;
    ECU_UART1_Rx_Add = (u32)Uart1_Rx_Data;

    BSW_DMAMUX_INIT(Uart1_TxDma.DMA_Ch,EDMA_REQ_LPUART1_TX);
    Uart1_TxDma.SrcAddr = ECU_UART1_Tx_Add;
    Uart1_TxDma.DestAddr = (u32)&(LPUART1->DATA);
    BSW_DMA_INIT_CFG(Uart1_TxDma);

    BSW_DMAMUX_INIT(Uart1_RxDma.DMA_Ch,EDMA_REQ_LPUART1_RX);
    Uart1_RxDma.SrcAddr = (u32)&(LPUART1->DATA);
    Uart1_RxDma.DestAddr = ECU_UART1_Rx_Add;
    BSW_DMA_INIT_CFG(Uart1_RxDma);

#endif
}



//BSW_DMA_REINIT(I2C0_TxDma.DMA_Ch,ECU_IIC_Tx_Add,(u32)(&(LPI2C0->MTDR)),DMA_TX_Lenth);
//BSW_DMA_START(I2C0_TxDma.DMA_Ch);

u8 G_u8_Rx_Flg = 0;
u8 G_u8_Tx_Flg = 0;
void ECU_UART_DMA_Tdata(void)
{
// LPUART1->WATER LPUART1->DATA LPUART1->STAT
	
	

	//if(G_u8_Rx_Flg)
	{
        //uart0
        Uart0_Tx_Data[0]++;
        BSW_DMA_REINIT(Uart0_TxDma.DMA_Ch,ECU_UART0_Tx_Add,(u32)(&(LPUART0->DATA)),5);
        BSW_DMA_START(Uart0_TxDma.DMA_Ch);

        //uart1
        Uart1_Tx_Data[0]++;
        BSW_DMA_REINIT(Uart1_TxDma.DMA_Ch,ECU_UART1_Tx_Add,(u32)(&(LPUART1->DATA)),5);
        BSW_DMA_START(Uart1_TxDma.DMA_Ch);

        //BSW_DMA_REINIT(Uart1_RxDma.DMA_Ch,(u32)(&(LPUART1->DATA)),ECU_UART1_Rx_Add,5);
        //BSW_DMA_START(Uart1_RxDma.DMA_Ch);
        G_u8_Rx_Flg = 0;
        //SystemSoftwareReset();
    }
}

inline void ECU_UART_DMA8_IRQ(void)
{
	G_u8_Rx_Flg = 1;
//	BSW_DMA_REINIT(Uart1_TxDma.DMA_Ch,ECU_UART_Rx_Add +1,(u32)(&(LPUART1->DATA)),5);
//	BSW_DMA_START(Uart1_TxDma.DMA_Ch);
    uart1_dmarxirq++;
}

inline void ECU_UART_DMA9_IRQ(void)
{
	G_u8_Tx_Flg = 1;
    uart1_dmatxirq++;
}

inline void ECU_UART_DMA10_IRQ(void)
{
//	BSW_DMA_REINIT(Uart1_TxDma.DMA_Ch,ECU_UART1_Rx_Add +1,(u32)(&(LPUART1->DATA)),5);
//	BSW_DMA_START(Uart1_TxDma.DMA_Ch);
    uart0_dmarxirq++;
}

inline void ECU_UART_DMA11_IRQ(void)
{
    uart0_dmatxirq++;
}


